package com.example.paimengshop;

import com.example.paimengshop.client_socket.ActionControl_Send;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Objects;
import java.util.ResourceBundle;

public class centerController implements Initializable {

    @FXML
    private AnchorPane myPane;

    @FXML
    private Button exitButton;

    @FXML
    private Button collectionButton;

    @FXML
    private Button orderButton;

    @FXML
    private Button resetButton;

    @FXML
    private ImageView faceImage;

    @FXML
    private Label nameLabel;

    @FXML
    private TextField nameField;

    @FXML
    private TextField addressField;

    @FXML
    private Label addressLabel;

    @FXML
    private HBox btn;

    @FXML
    private Button yesBtn;

    @FXML
    private Button noBtn;

    @FXML
    void yesOn(ActionEvent event) {

        if(!Objects.equals(nameField.getText(), "")){
            nameLabel.setText(nameField.getText());

            Data.currentUser.setUserName(nameField.getText());

            main.getSend().Send(ActionControl_Send.ChangeUserName(Data.getCurrentUser(), nameField.getText()));
        }

        if(!Objects.equals(addressField.getText(), "")){
            addressLabel.setText(addressField.getText());

            Data.currentUser.setAddress(addressField.getText());

            main.getSend().Send(ActionControl_Send.ChangeAddress(Data.currentUser, addressField.getText()));
        }

        nameLabel.setText("名称："+Data.currentUser.getUserName());
        addressLabel.setText("收货地址："+Data.currentUser.getAddress());

        nameLabel.setVisible(true);
        nameLabel.setManaged(true);
        nameField.setVisible(false);
        nameField.setManaged(false);
        addressLabel.setVisible(true);
        addressLabel.setManaged(true);
        addressField.setVisible(false);
        addressField.setManaged(false);
        btn.setVisible(false);
    }

    @FXML
    void noOn(ActionEvent event) {
        nameField.clear();
        addressField.clear();
        nameLabel.setVisible(true);
        nameLabel.setManaged(true);
        nameField.setVisible(false);
        nameField.setManaged(false);
        addressLabel.setVisible(true);
        addressLabel.setManaged(true);
        addressField.setVisible(false);
        addressField.setManaged(false);
        btn.setVisible(false);
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

        myPane.getStylesheets().add(getClass().getResource("All.css").toExternalForm());

        exitButton.getStyleClass().add("btn-basic");
        collectionButton.getStyleClass().add("btn-basic");
        yesBtn.getStyleClass().add("btn-basic");
        noBtn.getStyleClass().add("btn-basic");
        orderButton.getStyleClass().add("btn-basic");
        resetButton.getStyleClass().add("btn-basic");

        //组件设置
        faceImage.setImage(new Image(Data.currentUser.getHeadImageURL()));

        nameLabel.setText("名称："+Data.currentUser.getUserName());
        addressLabel.setText("收货地址："+Data.currentUser.getAddress());

        //nameField默认隐藏起来
        nameField.setVisible(false);
        nameField.setManaged(false);
        addressField.setVisible(false);
        addressField.setManaged(false);
        btn.setVisible(false);
        //鼠标点击事件

        //点击名字改名
        nameLabel.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                nameLabel.setVisible(false);
                nameLabel.setManaged(false);
                nameField.setVisible(true);
                nameField.setManaged(true);
                btn.setVisible(true);
            }
        });

        addressLabel.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                addressLabel.setVisible(false);
                addressLabel.setManaged(false);
                addressField.setVisible(true);
                addressField.setManaged(true);
                btn.setVisible(true);
            }
        });

        //点击头像选择文件改头像
        faceImage.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                File file=new FileChooser().showOpenDialog(null);

                if(file != null)
                {
                    faceImage.setImage(new Image(file.getAbsolutePath()));
                    Data.currentUser.setHeadImageURL(file.getAbsolutePath());

                    main.getSend().Send(ActionControl_Send.ChangeHeadImageURL(Data.getCurrentUser(), file.getPath()));
                }

            }
        });


        //进入订单
        orderButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Stage oldStage=(Stage) exitButton.getScene().getWindow();

                FXMLLoader fxmlLoader=new FXMLLoader(this.getClass().getResource("order.fxml"));

                Scene scene= null;
                try {
                    scene = new Scene(fxmlLoader.load());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

                Stage newStage=new Stage();
                newStage.setTitle("派蒙的周边商铺");
                newStage.setScene(scene);

//                oldStage.close();
                newStage.show();
            }
        });

        //进入收藏夹
        collectionButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Stage oldStage=(Stage) exitButton.getScene().getWindow();

                FXMLLoader fxmlLoader=new FXMLLoader(this.getClass().getResource("collection.fxml"));

                Scene scene= null;
                try {
                    scene = new Scene(fxmlLoader.load());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

                Stage newStage=new Stage();
                newStage.setTitle("派蒙的周边商铺");
                newStage.setScene(scene);

//                oldStage.close();
                newStage.show();
            }
        });

        //退出登录
        exitButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Stage oldStage=(Stage) exitButton.getScene().getWindow();

                FXMLLoader fxmlLoader=new FXMLLoader(this.getClass().getResource("mainUnlogin.fxml"));

                Scene scene= null;
                try {
                    scene = new Scene(fxmlLoader.load());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

                Stage newStage=new Stage();
                newStage.setTitle("派蒙的周边商铺");
                newStage.setScene(scene);

                oldStage.close();
                newStage.show();
            }
        });

        //修改密码
        resetButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                //点击跳转登录界面
                Stage oldStage=(Stage) exitButton.getScene().getWindow();

                FXMLLoader fxmlLoader=new FXMLLoader(this.getClass().getResource("Reset-View.fxml"));

                Scene scene= null;
                try {
                    scene = new Scene(fxmlLoader.load());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

                Stage newStage=new Stage();
                newStage.setTitle("派蒙的周边商铺");
                newStage.setScene(scene);

//                oldStage.close();
                newStage.show();
            }
        });
    }
}
